package com.ziven.common.widget.blur;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;

import com.ziven.common.widget.WidgetUtils;
import com.ziven.easygo.util.ResourceUtils;

/**
 * @author Ziven
 */
public final class BlurUtils {

    public static final int BLUR_RADIUS = 25;
    public static final int BLUR_SCALE = 8;

    private BlurUtils() {}

    @Nullable
    @WorkerThread
    public static Bitmap blurScaleWithDrawableSize(Drawable drawable) {
        return blurScaleWithDrawableSize(drawable, BLUR_RADIUS, BLUR_SCALE);
    }

    @Nullable
    @WorkerThread
    public static Bitmap blurScaleWithScreenSize(Drawable drawable) {
        return blurScaleWithScreenSize(drawable, BLUR_RADIUS, BLUR_SCALE);
    }

    @Nullable
    @WorkerThread
    public static Bitmap blurScaleWithDrawableSize(Drawable drawable, int radius, int scale) {
        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        return blurWithInWidthHeight(drawable, width, height, radius, scale);
    }

    @Nullable
    @WorkerThread
    public static Bitmap blurScaleWithScreenSize(Drawable drawable, int radius, int scale) {
        int[] screen = WidgetUtils.getScreenSize();
        return blurWithInWidthHeight(drawable, screen[0], screen[1], radius, scale);
    }

    @Nullable
    @WorkerThread
    public static Bitmap blurWithInWidthHeight(Drawable drawable, int inWidth, int inHeight, int radius, int scale) {
        if(scale <= 0) {
            scale = 1;
        }
        int outWidth = inWidth / scale;
        int outHeight = inHeight / scale;
        return blurWithOutWidthHeight(drawable, outWidth, outHeight, radius);
    }

    @Nullable
    @WorkerThread
    public static Bitmap blurWithOutWidthHeight(Drawable drawable, int outWidth, int outHeight, int radius) {
        Bitmap sizeBitmap = ResourceUtils.drawable2Bitmap8888(drawable, outWidth, outHeight);
        return blurCanReuseInBitmap(sizeBitmap, radius);
    }

    @Nullable
    @WorkerThread
    public static Bitmap blurCanReuseInBitmap(Bitmap inBitmap, int radius) {
        return blur(inBitmap, radius, true);
    }

    @Nullable
    @WorkerThread
    public static Bitmap blurCanNotReuseInBitmap(Bitmap inBitmap, int radius) {
        return blur(inBitmap, radius, false);
    }

    @Nullable
    @WorkerThread
    public static Bitmap blur(Bitmap inBitmap, int radius, boolean canReuseInBitmap) {
        if(ResourceUtils.hasRecycled(inBitmap)) {
            return null;
        }
        Bitmap bitmap;
        if (canReuseInBitmap) {
            bitmap = inBitmap;
        } else {
            bitmap = inBitmap.copy(inBitmap.getConfig(), true);
        }

        Bitmap outBitmap = null;
        try {
            outBitmap = RSBlur.blur(ResourceUtils.getContext(), bitmap, radius);
        } catch (Throwable th1) {
            Log.e("BlurUtils", "RSBlur", th1);
            try {
                outBitmap = FastBlur.blur(bitmap, radius, true);
            } catch (Throwable th2) {
                Log.e("BlurUtils", "FastBlur", th2);
            }
        }
        return outBitmap;
    }
}
